package cn.b.sky.form.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.form.model.Sequence;
import cn.b.sky.form.model.condition.SequenceCondition;
import cn.b.sky.form.service.SequenceService;
import cn.b.sky.user.controller.SecurityController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <strong>Title : SequenceController.java<br></strong>
 * <strong>Package : cn.b.sky.flowable.controller<br></strong>
 * <strong>Description : </strong>序列操作控制器<br> 
 * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
 * <p>
 * @author JunLin.Yang<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/sequence/*")
public class SequenceController extends SecurityController {

  @Resource
  SequenceService sequenceService;

  /**
   * 
   * <strong>Title : listSequencesPageInfo<br></strong>
   * <strong>Description : </strong>根据查询条件查询序列分页信息<br> 
   * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param condition 序列查询条件
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "listSequencesPageInfo")
  public String listSequencesPageInfo(String navTabId, @ModelAttribute("condition") SequenceCondition condition,
      Model model) {
    condition.setSiteCode(this.getSiteCode());
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    model.addAttribute("page", this.sequenceService.selectSequencePageByCondition(condition));
    model.addAttribute(NAVTABID, navTabId);
    return "workflow/sequence_list";
  }

  /**
   * 
   * <strong>Title : lookUpSequencesPageInfo<br></strong>
   * <strong>Description : </strong>根据查询条件查询序列分页信息<br> 
   * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param condition 序列查询条件
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "lookUpSequencesPageInfo")
  public String lookUpSequencesPageInfo(String navTabId, @ModelAttribute("condition") SequenceCondition condition,
      Model model) {
    condition.setSiteCode(this.getSiteCode());
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    model.addAttribute("page", this.sequenceService.selectSequencePageByCondition(condition));
    model.addAttribute(NAVTABID, navTabId);
    return "workflow/sequence_lookup";
  }

  /**
   * 
   * <strong>Title : viewSequence<br></strong>
   * <strong>Description : </strong>根据主键查询序列对象<br> 
   * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param sequenceCode 序列编号
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "viewSequence")
  public String viewSequence(String navTabId, String sequenceCode, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "view");
    model.addAttribute("sequence", this.sequenceService.getSequenceByPrimaryKey(this.getSiteCode(), sequenceCode));
    return "workflow/sequence_add";
  }

  /**
   * 
   * <strong>Title : toAddSequence<br></strong>
   * <strong>Description : </strong>新增序列信息前置事件<br> 
   * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "toAddSequence")
  public String toAddSequence(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    return "workflow/sequence_add";
  }

  /**
   * 
   * <strong>Title : toEditSequence<br></strong>
   * <strong>Description : </strong>修改序列信息前置事件<br> 
   * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param sequenceCode 序列编号
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "toEditSequence")
  public String toEditSequence(String navTabId, String sequenceCode, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    model.addAttribute("sequence", this.sequenceService.getSequenceByPrimaryKey(this.getSiteCode(), sequenceCode));
    return "workflow/sequence_edit";
  }

  /**
   * 
   * <strong>Title : validateSequence<br></strong>
   * <strong>Description : </strong>根据主键查询序列对象是否存在<br> 
   * <strong>Create on : 2016-9-28 上午10:37:43<br></strong>
   * <p>
   * @param sequenceCode
   * @return
   * boolean
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("validateSequence")
  @ResponseBody
  public boolean validateSequence(String sequenceCode) {
    if (null == sequenceCode) {
      return true;
    }
    return !this.sequenceService.existSequenceByPrimaryKey(this.getSiteCode(), sequenceCode.toUpperCase());
  }

  /**
   * 
   * <strong>Title : saveSequence<br></strong>
   * <strong>Description : </strong>新增或修改序列信息<br> 
   * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param sequence 序列
   * @return
   * DWZResult 操作结果
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "saveSequence")
  @ResponseBody
  public DWZResult saveSequence(String navTabId, Sequence sequence) {
    if (null == sequence) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    sequence.setSiteCode(getSiteCode());
    if (!this.sequenceService.existSequenceByPrimaryKey(sequence.getSiteCode(), sequence.getSequenceCode())) {
      setCreateInfo(sequence);
      this.sequenceService.insertSequence(sequence);
    } else {
      setModifyInfo(sequence);
      this.sequenceService.updateSequence(sequence);
    }
    return dialogAjaxDone(navTabId, null, null, null);
  }

  /**
   * 
   * <strong>Title : deleteSequenceBatch<br></strong>
   * <strong>Description : </strong>删除序列信息<br> 
   * <strong>Create on : 2016-09-28 上午09:36:14<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param model 页面值对象
   * @param request 页面请求对象
   * @return
   * DWZResult 操作结果
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "deleteSequenceBatch")
  @ResponseBody
  public DWZResult deleteSequenceBatch(String navTabId, Model model, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String siteCode = this.getSiteCode();
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = this.sequenceService.deleteSequenceByPrimaryKey(siteCode, res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }

}
